<?php
class SettingControl extends AdminModule
{
	public $layout = false;

	//站点管理，列表
	public function actionSite()
	{
		$this->render('setting/site');
	}

	//站点管理菜单
	public function actionSiteMenu($url = '')
	{
		$this->ajaxReturn($this->getCreateButton($url, '添加站点'), 'text');
	}


	//新增站点
	public function actionSiteAdd()
	{
		$encrypt_key  = D::$config['encrypt'];
		if (isset($_POST['token'])) {
			if ($this->encrypt->decode($_POST['token'], $encrypt_key . '_addsite') != 'site_info') {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_1']) );
			} else {
				if (siteModel::model()->insert()) {
					$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_3'], D::createUrl('admin/setting/site')) );
				} else {
					$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_5']) );
				}
			}
		}
		$var = array(
			'token_code' => $this->encrypt->encode('site_info', $encrypt_key.'_addsite'),
		);
		$this->render('setting/site_info', $var);
	}

	//新增站点菜单
	public function actionSiteAddMenu($url = '')
	{
		$this->ajaxReturn($this->getSaveButton($url, 'site'), 'text');
	}



	//修改站点
	public function actionSiteEdit($site_id = 1)
	{
		$encrypt_key  = D::$config['encrypt'];
		if (isset($_POST['token'])) {
			if ($this->encrypt->decode($_POST['token'], $encrypt_key . '_editsite') != 'site_info') {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_1']) );
			} else {
				if (siteModel::model()->updateByPk($_POST['site_id'])) {
					$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_1'], D::createUrl('admin/setting/site')) );
				} else {
					$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_3']) );
				}
			}
		} else if (empty($_POST)) {
			$var = array(
				'token_code' => $this->encrypt->encode('site_info', $encrypt_key.'_editsite'),
				'cfg'		 => siteModel::model()->getSiteInfo((int)$site_id)
			);
			$this->render('setting/site_info', $var);
		}
	}

	//修改站点菜单
	public function actionSiteEditMenu($url = '')
	{
		$this->ajaxReturn($this->getSaveButton($url, 'site'), 'text');
	}


	//删除站点
	public function actionSiteDelete($site_id)
	{
		if (empty($site_id)) {
			$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_2']) );
		} else {
			if (siteModel::model()->deletes(array('IN', 'site_id', explode(',', $site_id)))) {
				$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_2']) );
			} else {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_4']) );
			}
		}
	}

	//全局设置
	public function actionGlobal()
	{
		$encrypt_key  = D::$config['encrypt'];
		if (isset($_POST['token'])) {
			if ($this->encrypt->decode($_POST['token'], $encrypt_key . '_global') != 'global') {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_1']) );
			} else {

				$url_mode   = $_POST['url_mode'];
				$url_route 	= $_POST['url_route'];
				$url_suffix = trim(htmlspecialchars($_POST['url_suffix']));
				$file404 	= trim(htmlspecialchars($_POST['file404']));
				$custom = file_get_contents(APP_PATH . '/app/config/custom.php');
				$this->_saveConfig('url_route',  $url_route,  $custom);
				$this->_saveConfig('url_mode',   $url_mode,   $custom);
				$this->_saveConfig('url_suffix', $url_suffix, $custom);
				$this->_saveConfig('file404',    $file404,    $custom);
				file_put_contents(APP_PATH . '/app/config/custom.php', $custom);
				if (settingModel::model()->saveData()) {
					$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_1'], D::createUrl('admin/setting/global')) );
				} else {
					$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_3']) );
				}
			}
		} else if (empty($_POST)) {
			$config = D::import('app:config.config', true);
			$custom['url_mode']   = $config['access']['url_mode'];
			$custom['url_suffix'] = $config['access']['url_suffix'];
			$custom['url_route']  = $config['access']['url_route'];
			$custom['file404'] 	  = $config['access']['file404'];
			unset($config);
			$setting = settingModel::model()->getSetting();
			$var = array(
				'token_code' => $this->encrypt->encode('global', $encrypt_key.'_global'),
				'set'		 => array_merge($setting, $custom),
			);
			$this->render('setting/global', $var);
		}
	}

	//全局设置站点菜单
	public function actionGlobalMenu($url = '')
	{
		$this->ajaxReturn($this->getSaveButton($url, 'global'), 'text');
	}

	//执行sql语句
	public function actionSql()
	{
		echo 'sql';
	}

	//程序升级
	public function actionUpdate()
	{
		echo 'update';
	}

	//清理缓存
	public function actionClearCache()
	{
		echo 'clear_cache';
	}

	private function _saveConfig($name, $value, &$custom) {
		$custom = preg_replace(
			'/\[\''.$name.'\'\](\s*=\s*)[\'|\"]*(.*)[\'|\"]*;/iu',
			"['".$name."']\\1'".$value."';",
			$custom
		);
	}
}
?>